package com.base.cn.platform.os.service.op.affiche.tmp;

import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.op.affiche.OpReceiveAffiche;
import com.base.cn.platform.os.entity.user.account.CusUserAccountHistory;
import com.base.cn.platform.os.entity.user.account.CusUserCashRecord;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.entity.user.user.CuserProfile;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提现消息提醒
 *
 * @author s.li
 * @date 2019/05/29
 */
@Component
public class CashSendTemAffice extends SendTemAffice {

    @Override
    public Map<String, Object> initMsg(Object[] arr, WXTmpEnum tmpEnum, List<CusUser> userList, Map<BigDecimal, List> dataListMap) {
        initService(arr);

        String accountCashVarNum1 = smsTempConfigMap.get("accountCashVarNum1");
        String accountCashVarNum2 = smsTempConfigMap.get("accountCashVarNum2");
        String accountCashRemark = smsTempConfigMap.get("accountCashRemark");
        String accountCashMsgType = smsTempConfigMap.get("accountCashMsgType");

        if(StringUtils.isNotEmpty(accountCashVarNum1)
                && StringUtils.isNotEmpty(accountCashVarNum2)
                && StringUtils.isNotEmpty(accountCashMsgType)){
            //提现记录
            CusUserCashRecord cashRecord = (CusUserCashRecord)dataListMap.get(userList.get(0).getId()).get(0);

            Map<String,Object> msg = new HashMap<>();
            boolean status = false;
            if(accountCashMsgType.contains(MsgType.SITE.getKey())){
                List<OpReceiveAffiche> receiveAfficheList = new ArrayList<>();
                userList.forEach(user -> {
                    OpReceiveAffiche affiche = new OpReceiveAffiche();
                    affiche.setMsgType(1);
                    affiche.setStatus(1);
                    affiche.setUserId(user.getId());
                    StringBuilder feedbackContext = new StringBuilder();
                    feedbackContext.append(cashRecord.getStatus()==2?"提现成功通知":"提现失败通知");
                    feedbackContext.append(accountCashVarNum1.replace("{1}", cashRecord.getStatus()==2?"成功":"失败"));
                    feedbackContext.append(" , ");
                    feedbackContext.append(accountCashVarNum2.replace("{2}", "￥"+String.valueOf(cashRecord.getPrice())));
                    feedbackContext.append(" , ");
                    if(StringUtils.isNotEmpty(accountCashRemark)){
                        feedbackContext.append(" , ");
                        feedbackContext.append(accountCashRemark);
                    }
                    affiche.setContext(feedbackContext.toString());
                    receiveAfficheList.add(affiche);
                });
                msg.put(SITE_MESSAGE_KEY,receiveAfficheList);
                status = true;
            }
            if(accountCashMsgType.contains(MsgType.WX.getKey())){
                List<CuserProfile> cuserProfileList =this.getCuserProfileList(userList);
                if(ObjectUtils.isNotEmpty(cuserProfileList)){
                    String tmpId = getTmpId(tmpEnum);
                    List<JSONObject> jsonObjectList = new ArrayList<>();
                    cuserProfileList.forEach(cuserProfile -> {
                        //设置参数
                        JSONObject obj = new JSONObject();
                        obj.put("touser", cuserProfile.getOpenId());
                        obj.put("template_id", tmpId);
                        //微信消息数据内容
                        Map<String,Map<String,String>> data = new HashMap<>();

                        //消息标题
                        Map<String,String> first = new HashMap<>();
                        first.put("value",cashRecord.getStatus()==2?"提现成功通知":"提现失败通知");
                        first.put("color","#173177");
                        data.put("first",first);

                        Map<String,String> keyword1 = new HashMap<>();
                        keyword1.put("value",cashRecord.getStatus()==2?"成功":"失败");
                        keyword1.put("color","#173177");
                        data.put("keyword1",keyword1);

                        Map<String,String> keyword2 = new HashMap<>();
                        keyword2.put("value", "￥"+String.valueOf(cashRecord.getPrice()));
                        keyword2.put("color","#173177");
                        data.put("keyword2",keyword2);

                        this.setRemarkMap(data,accountCashRemark);
                        obj.put("data", data);
                        jsonObjectList.add(obj);
                    });
                    msg.put(WX_MESSAGE_KEY,jsonObjectList);
                    status = true;
                }
            }
            if(status){
                msg.put(MSG_TYPE,accountCashMsgType);
                return msg;
            }
        }
        return null;
    }
}
